# -*- coding: utf-8 -*-
# -*- test-case-name: pytils.test.test_typografica -*-
# PyTils - simple processing for russian strings
# Copyright (C) 2006-2007  Yury Yurevich
#
# http://gorod-omsk.ru/blog/pythy/projects/pytils/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
"""
Nice typografical look for texts
"""

__id__ = __revision__ = "$Id: typografica.py 112 2007-10-18 12:46:03Z the.pythy $"
__url__ = "$URL: http://pythy.googlecode.com/svn/branches/pytils-typografica-16/pytils/typografica.py $"

import re

DEFAULT_SETTINGS = {
    'inches': True,     # преобразовывать дюймы в знак дюйма
    'laquo': True,      # кавычки-ёлочки
    'quotes': True,     # кавычки английские лапки
    'dash': True,       # короткое тире (150)
    'emdash': True,     # длинное тире двумя минусам (151)
    'initials': True,   # тонкие шпации в инициалах
    'copyright': True,  # заменять (c), (r), (tm) на знаки копирайтов
    'plus_minus': True, # заменять +-
    'degrees': True,    # знак градуса
    'dashglue': True,   # приклеивание предлогов
    'wordglue': True,   # приклеивание дефисов
    'spacing': True,    # запятые и пробелы, перестановка
    'ellipsis': True,   # троеточия
    }

# глифы
GLYPHS = {
    'nbsp': u'\u00a0',
    'thinsp': u' ',
    'laquo': u'«',
    'raquo': u'»',
    'sect': u'§',
    'copy': u'©',
    'reg': u'®',
    'deg': u'°',
    'pm': u'±',
    'para': u'¶',
    'middot': u'·',
    'ndash': u'–',
    'mdash': u'—',
    'lsquo': u'‘',
    'rsquo': u'’',
    'ldquo': u'“',
    'rdquo': u'”',
    'bdquo': u'„',
    'bull': u'•',
    'hellip': u'…',
    'number': u'№',
    'tm': u'™',
    'minus': u'−',
    'inch': u'″',
    }

class Formatter(object):
    def __init__(self, **settings):
        self.filters = []
        self.settings = DEFAULT_SETTINGS.copy()
        self.settings.update(settings)
        try:
            for option, value in self.settings.items():
                if value is True:
                    self.filters.append(getattr(self, "process_%s" % option))
        except AttributeError:
            raise ValueError("Unknown option %s in settings" % option)
        self.init_filters()

    def init_filters(self):
        """
        Initialize filters
        """
        for option, value in self.settings.items():
            if value is True:
                init_filter = getattr(self, "init_%s" % option, False)
                if init_filter:
                    init_filter()

    @staticmethod
    def process_ellipsis(text):
        return re.sub(u'...', GLYPHS['hellip'])
    
    def process_inches(self, text):
        return text

    def process_laquo(self, text):
        return text

    def process_quotes(self, text):
        return text

    def process_dash(self, text):
        return text

    def process_emdash(self, text):
        return text

    def process_initials(self, text):
        return text

    def process_copyright(self, text):
        return text

    def process_plus_minus(self, text):
        return text

    def process_degrees(self, text):
        return text

    def process_dashglue(self, text):
        return text
    
    def process_wordglue(self, text):
        return text

    def process_spacing(self, text):
        return text
